﻿@model HZYProject.Domain.Exam

<input type="hidden" id="ExamID" name="ExamID" value="@Model.ExamID" />
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>
<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchDelete">
        <i class="layui-icon layui-icon-delete"></i>
        批量删除
    </button>
</script>
<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-sm" lay-event="reset"><i class="layui-icon layui-icon-refresh-1"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>
@section script{
    <script>
        layui.use(['table', 'form', 'jquery'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;

            let MODULE_PATH = "/Exam";

            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '序号',
                    align: 'left',
                    type: "numbers"
                },
                {
                    title: '学生编号',
                    field: 'studentID',
                    align: 'left'
                },
                {
                    title: '成绩编号',
                    field: 'scoreID',
                    align: 'left'
                },
                {
                    title: '考试编号',
                    field: 'examID',
                    align: 'left'
                },
                {
                    title: '真实姓名',
                    field: 'realName',
                    align: 'left'
                },
                {
                    title: '身份证号',
                    field: 'idNo',
                    align: 'left'
                },
                {
                    title: '成绩',
                    field: 'score',
                    align: 'left'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'left',
                    width: 160,
                    fixed: 'right'
                }]
            ]
            var eid = $("#ExamID").val();

            table.render({
                elem: '#user-table',
                url: '/Exam/ExamPerson',
                method: 'post',
                where: {
                    "ExamID": eid
                },
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#user-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
                , done: function () {
                    $("[data-field='studentID']").css('display', 'none');
                    $("[data-field='examID']").css('display', 'none');
                    $("[data-field='scoreID']").css('display', 'none');
                }
            });

            table.on('toolbar(user-table)', function (obj) {
                if (obj.event === 'batchDelete') {
                    window.batchDelete(obj);
                } else if (obj.event === 'refresh') {
                    window.refresh();
                }
            });

            table.on('tool(user-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'reset') {
                    window.reset(obj);
                }
            });

            window.remove = function (obj) {
                layer.confirm('确定要删除吗？请谨慎操作。', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + "/removeStu/" + obj.data['scoreID'],
                        method: 'post',
                        dataType: 'json',
                        type: 'delete',
                        success: function (result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    obj.del();
                                    window.location.reload();
                                });
                            } else {
                                layer.msg("删除失败：" + result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.reset = function (obj) {
                layer.confirm('您确定要重置该学生本次考试吗？请谨慎操作。', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + "/resetStu/" + obj.data['scoreID'],
                        method: 'post',
                        dataType: 'json',
                        success: function (result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg("删除失败：" + result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.batchDelete = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = "";
                for (let i = 0; i < data.length; i++) {
                    ids += data[i].scoreID + ",";
                }
                ids = ids.substr(0, ids.length - 1);
                console.log(ids);
                layer.confirm('确定要删除这些学生吗', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + "/batchDeleteStu",
                        method: 'post',
                        dataType: 'json',
                        data: {
                            'ids': ids
                        },
                        success: function (result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function (param) {
                table.reload('user-table');
            }

        })
    </script>
}
